package com.koron.css2.projectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.projectCenter.bean.PjInternalBean;
import com.koron.css2.projectCenter.bean.query.PjInternalQueryBean;
import com.koron.css2.projectCenter.dto.*;
import com.koron.css2.projectCenter.mapper.PjInternalMapper;
import com.koron.css2.projectCenter.vo.PjInternalDetailVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.systemManage.bean.OrgUserBean;
import com.koron.css2.systemManage.mapper.OrgUserMapper;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 内部工程详情
 * @author 甄玉晴
 * 2020年5月19日
 */
public class PjInternalQuery implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			//将入参转换为QueryBean
			PjInternalQueryBean queryBean = JsonUtils.objectToPojo(req.getData(), PjInternalQueryBean.class);
			if(StringUtils.isBlank(queryBean.getId())) {
				return MessageBean.create(Constant.NOT_NULL, "主键不能为空。", null);
			}
			
			//查询内部工程
			PjInternalMapper mapper = factory.getMapper(PjInternalMapper.class);
			PjInternalBean pjInternalBean = mapper.selectById(queryBean.getId());
			if (pjInternalBean == null) {
				return MessageBean.create(Constant.NOT_NULL, "用水安装单据不存在。", null);
			}
	
			//输出VO组装，按节点拆开
			PjInternalDetailVO detailVO = new PjInternalDetailVO();
			//转单据信息
			CommonUtils.copyPropertiesIgnoreNull(pjInternalBean, detailVO);
			//转登记信息
			PjInternalRegisterDto registerDto = new PjInternalRegisterDto();
			CommonUtils.copyPropertiesIgnoreNull(pjInternalBean, registerDto);
			detailVO.setRegister(registerDto);
			//数据字典转换
			//convert(factory, detailVO, userInfo);
			//转勘察设计
			PjInstallSurveyDesignDto surveyDesign = new PjInstallSurveyDesignDto();
			CommonUtils.copyPropertiesIgnoreNull(pjInternalBean, surveyDesign);
			detailVO.setSurveyDesign(surveyDesign);
			//转工程预算
			PjInternalBudgetDto budget = new PjInternalBudgetDto();
			CommonUtils.copyPropertiesIgnoreNull(pjInternalBean, budget);
			detailVO.setBudget(budget);;
			//转安装施工
			PjInternalInstallDto install = new PjInternalInstallDto();
			CommonUtils.copyPropertiesIgnoreNull(pjInternalBean, install);
			detailVO.setInstall(install);
			//转工程验收
			PjInternalCheckDto check = new PjInternalCheckDto();
			CommonUtils.copyPropertiesIgnoreNull(pjInternalBean, check);
			detailVO.setCheck(check);
			//转工程结算
			PjInternalSetmtDto setmt = new PjInternalSetmtDto();
			CommonUtils.copyPropertiesIgnoreNull(pjInternalBean, setmt);
			detailVO.setSetmt(setmt);
			convert(factory, detailVO, userInfo);
			//判断是否审批节点
			detailVO.setApproveFlag(0);
			if(!StringUtils.isBlank(detailVO.getProcessState()) 
					&& ("CANCEL".equals(detailVO.getProcessState())
							|| "END".equals(detailVO.getProcessState())
							|| isApproval(detailVO.getProcessState()))) {
				//detailVO.setProcessState(detailVO.getProcessState().replace("-approval", ""));
				detailVO.setApproveFlag(1);
			}
			//输出分页结果
			MessageBean<PjInternalDetailVO> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PjInternalDetailVO.class);
			info.setData(detailVO);
			return info;
		} catch (Exception e) {
			logger.error("内部工程详情查询失败"+e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
		}
	}
	/**
	 * 判断流程状态
	 */
	public boolean isApproval(String process) {
		if(!"register".equals(process) && !"surveyDesign".equals(process) && !"budget".equals(process)
				&& !"install".equals(process)&& !"check".equals(process)&& !"setmt".equals(process)) {
			return true;
		}
		return false;
		
	}
	/**
	 * 数据字典转换
	 * @param vo
	 */
	private void convert(SessionFactory factory, PjInternalDetailVO vo, UserInfoBean userInfo) {

		//内部工程登记信息
		PjInternalRegisterDto register = vo.getRegister();
		PjInstallSurveyDesignDto surveyDesign = vo.getSurveyDesign();
		PjInternalBudgetDto budget = vo.getBudget();
		PjInternalCheckDto check = vo.getCheck();
		PjInternalSetmtDto setmt = vo.getSetmt();
		//工程类型PPT
		register.setPjTypeName(Tools.getDicNameByCodeAndValue(factory, "PPT", register.getPjType()));
		//紧急程度EGD
		register.setUrgentFlag(Tools.getDicNameByCodeAndValue(factory, "EGD", register.getUrgentFlag()));
		//勘察节点
		OrgUserMapper omapper = factory.getMapper(OrgUserMapper.class);
		//勘察人员
		if(surveyDesign != null && !StringUtils.isBlank(surveyDesign.getSurveyName())) {
			OrgUserBean user = omapper.selectByAccount(surveyDesign.getSurveyName());
			surveyDesign.setSurveyNameName(user==null?"":user.getUserName());
		}
		//设计人员
		if(surveyDesign != null && !StringUtils.isBlank(surveyDesign.getDesignName())) {
			OrgUserBean user = omapper.selectByAccount(surveyDesign.getDesignName());
			surveyDesign.setDesignNameName(user==null?"":user.getUserName());
		}
		//预算节点编制人员
		if(budget != null && !StringUtils.isBlank(budget.getBudgetName())) {
			OrgUserBean user = omapper.selectByAccount(budget.getBudgetName());
			budget.setBudgetNameName(user==null?"":user.getUserName());
		}
		//验收节点验收人员
		if(check != null && !StringUtils.isBlank(check.getCheckName())) {
			OrgUserBean user = omapper.selectByAccount(check.getCheckName());
			check.setCheckNameName(user==null?"":user.getUserName());
		}
		//结算节点
		if(setmt != null && !StringUtils.isBlank(setmt.getSetmtCostSkname())) {
			OrgUserBean user = omapper.selectByAccount(setmt.getSetmtCostSkname());
			setmt.setSetmtCostSknameName(user==null?"":user.getUserName());
		}
	}
}
